﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBScript;
using eBlog.Core;
using eBlog.Entity;
using eBlog.Common;
using eBlog.Service;
using System.Collections;
namespace eBlog.Web.Admin
{
    public partial class PageManage : PageBase
    {
        IDBBase dal = DBFactory.GetDbProvider(); //初始化数据工厂
        protected void Page_Load(object sender, EventArgs e)
        {
            PageValidate.CheckAdminLogin(this);
            if (!Page.IsPostBack)
            {
                switch (action)
                {
                    case "add":
                        edit.Visible = true;
                        list.Visible = false;
                       
                        break;

                    case "edit":
                        edit.Visible = true;
                        list.Visible = false;
                     
                        ShowInfo();

                        break;
                    case "del":
                        delInfo();
                        break;
                    case "show":
                        edit.Visible = false;
                        list.Visible = false;
                       
                        show.Visible = true;
                        break;
                    default:
                        edit.Visible = false;
                        list.Visible = true;
                        BindList();
                        break;
                }
            }
            else
            {
                if (action == "del")
                    delInfo();
            }
        }

        private void BindList()
        {
            int PageIndex;
            if (!int.TryParse(Request.Params["page"], out PageIndex)) { PageIndex = 1; }

            ArrayList attrs = dal.Where("CodeName is not null").Page(PageIndex, PageSize).GetListAndCount<Posts>();

            List<Posts> lists = attrs[0] as List<Posts>;
            ListCount = (int)attrs[1];
            RPLIST.DataSource = lists;
            RPLIST.DataBind();
            PageNav.Text = Pager.PageInfo(ListCount, PageSize, PageIndex);
        }


        public void btnSave_Click(object sender, EventArgs e)
        {
            ArrayList strErrs = new ArrayList(); //Add时候必须用::分开控件名
            this.edit.Visible = true;
            if (this.txtTitle.Text == "")
            {
                strErrs.Add("txtTitle::Title不能为空，请输入字符！");
            }
         
            if (this.txtContent.Text == "")
            {
                strErrs.Add("txtContent::Content不能为空，请输入字符！");
            }
            if (this.txtCodeName.Text.Trim() == "")
            {
                strErrs.Add("txtContent::Content不能为空，请输入字符！");
            }
          
            if (strErrs.Count > 0)
            {
                string[] arrayErrs = (string[])strErrs.ToArray(typeof(string));
                string strErr = string.Join("\\n", arrayErrs);
                //MessageBox.Show(this,strErr);
                ErrCodeStr.Value = strErr;
                return;
            }

           
            string Title = this.txtTitle.Text;
            string Content = this.txtContent.Text;
            int CommentStatus = 1;
            if (this.chkCommentStatus.Checked)
            {
                CommentStatus = 0;
            }
            int TopStatus = 0;
            bool TopShow = false;
            bool recommend = false;
            string codename = this.txtCodeName.Text;
            if (action == "edit" && id > 0)
            {
                eBlog.Entity.Posts model = dal.GetModel<eBlog.Entity.Posts>(id); //为了不覆盖更新,先读出存在的数据再按需填充数据
                model.Title = Title;
                //model.Slug = Slug;
                //model.Imageurl = Imageurl;
               
                model.Content = Content;
                model.CodeName = codename;
                model.CommentStatus = CommentStatus;
              
                model.Status = 1;
                model.TopStatus = TopStatus;
                model.UpdateDate = DateTime.Now;
                model.TopShow = TopShow;
                model.Recommend = recommend;
                dal.Update<eBlog.Entity.Posts>(model);
                //MessageBox.ShowAndRedirect(this, "修 改 完 成!", Referrer.Value);
                return;
            }
            else
            {
                Posts model = new Posts();  //添加数据,准备新的实体

              
                model.Title = Title;
                //model.Slug = Slug;
                //model.Imageurl = Imageurl;
                model.Content = Content;
                model.CodeName = codename;
                model.CommentStatus = CommentStatus;
                model.CommentCount = 0;
                model.ViewCount = 0;
                model.Status = 1;
                model.Recommend = recommend;
                model.TopStatus = TopStatus;
                model.CreateDate = DateTime.Now;
                model.TopShow = TopShow;
                Response.Redirect(Request.Url.AbsolutePath + "#" + dal.Add<Posts>(model).ToString()); //ADD并 返回当前Uri绝对地址不带参数
                return;
            }

        }

        private void ShowInfo()
        {
            try { this.Referrer.Value = Request.UrlReferrer.PathAndQuery; }
            catch { }
            if (id > 0 && action == "edit")
            {
                Posts model = dal.GetModel<Posts>(id);
                this.txtTitle.Text = model.Title;
                //this.txtSlug.Text = model.Slug;
                //this.txtImageurl.Text = model.Imageurl;
               
                this.txtContent.Text = model.Content;
               
                if (model.CommentStatus == 0)
                {
                    this.chkCommentStatus.Checked = true;
                }
                this.btnSave.Text = "编 辑";
            }

        }




        private void delInfo()
        {
            string ids = PageValidate.Form("checkallid");
            if (!String.IsNullOrEmpty(ids))
            {
                //dal.From("vt_Posts", "").Where("id in (@ids)").SetString("ids", ids).Delete();
                dal.From("Posts", "").Where("id in (" + ids + ")").Delete();
            }
            else
            {
                dal.Delete<Posts>(id);
            }
         
            Response.Redirect(Request.Url.AbsolutePath); //返回当前Uri绝对地址不带参数
            return;
        }

      
    }
}